﻿using System;
using System.Collections.Generic;
using System.Linq;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.DynamicEntityProperties;
using Abp.UI;
using Microsoft.EntityFrameworkCore;

namespace Blog.DynamicPropertys
{
    /// <summary>
    /// 动态属性（DynamicEntityProperty）及动态属性值（DynamicEntityPropertyValue）扩展
    /// </summary>
    public class ExtDynamicEntityPropertyManager : ITransientDependency //TODO 定义一个接口
    {
        private readonly DynamicPropertyManager _dynamicPropertyManager;
        private readonly DynamicPropertyValueManager _dynamicPropertyValueManager;
        private readonly DynamicEntityPropertyManager _dynamicEntityPropertyManager;
        private readonly DynamicEntityPropertyValueManager _dynamicEntityPropertyValueManager;
        private readonly IRepository<DynamicProperty> _dynamicPropertyRepository;
        private readonly IRepository<DynamicPropertyValue> _dynamicPropertyValueRepository;
        private readonly IRepository<DynamicEntityProperty> _dynamicEntityPropertyRepository;
        private readonly IRepository<DynamicEntityPropertyValue> _dynamicEntityPropertyValueRepository;

        public ExtDynamicEntityPropertyManager(
            DynamicPropertyManager dynamicPropertyManager,
            DynamicPropertyValueManager dynamicPropertyValueManager,
            DynamicEntityPropertyManager dynamicEntityPropertyManager,
            DynamicEntityPropertyValueManager dynamicEntityPropertyValueManager,
            IRepository<DynamicProperty> dynamicPropertyRepository,
            IRepository<DynamicPropertyValue> dynamicPropertyValueRepository,
            IRepository<DynamicEntityProperty> dynamicEntityPropertyRepository,
            IRepository<DynamicEntityPropertyValue> dynamicEntityPropertyValueRepository
        )
        {
            _dynamicPropertyManager = dynamicPropertyManager;
            _dynamicPropertyValueManager = dynamicPropertyValueManager;
            _dynamicEntityPropertyManager = dynamicEntityPropertyManager;
            _dynamicEntityPropertyValueManager = dynamicEntityPropertyValueManager;
            _dynamicPropertyRepository = dynamicPropertyRepository;
            _dynamicPropertyValueRepository = dynamicPropertyValueRepository;
            _dynamicEntityPropertyRepository = dynamicEntityPropertyRepository;
            _dynamicEntityPropertyValueRepository = dynamicEntityPropertyValueRepository;
        }

        public DynamicEntityProperty GetEntityProperty(string propertyName, string entityFullName, int tenantId = 1)
        {
            var property = _dynamicPropertyRepository.FirstOrDefault(x =>
                    x.PropertyName == propertyName && x.TenantId == tenantId);
            if (property == null)
            {
                throw new Exception($"错误，无法找到名为[{propertyName}]的动态属性！(租户为[{tenantId}])");
            }

            var entityProperty = _dynamicEntityPropertyRepository.FirstOrDefault(x =>
                x.EntityFullName == entityFullName
                && x.TenantId == tenantId
                && x.DynamicPropertyId == property.Id
                );
            
            if (entityProperty == null)
            {
                throw new Exception($"错误，无法获取到实体为[{entityFullName}]的动态属性[{propertyName}]！");
            }
            return entityProperty;
        }
    }
}